package com.esoon.activity;

import android.Manifest;
import android.annotation.TargetApi;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.provider.Settings;
import android.support.annotation.NonNull;
import android.support.v4.app.ActivityCompat;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.RelativeLayout;
import android.widget.Toast;

import com.esoon.smartsee.MainActivity;
import com.esoon.smartsee.R;
import com.esoon.utils.AlertDialogCommon;
import com.esoon.utils.PermissionHelper;
import com.esoon.utils.PermissionInterface;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class LoginActivity extends AppCompatActivity {
    SharedPreferences sharedPreferences;
    SharedPreferences sharedPreferences1;
    SharedPreferences.Editor editor;
    SharedPreferences.Editor editor1;
    @BindView(R.id.loginBackLayout)
    RelativeLayout loginlayout;
    @BindView(R.id.userName)
    EditText userNameEdit;
    @BindView(R.id.password)
    EditText psdEdit;
    @BindView(R.id.setting_button)
    ImageButton setting_Button;

    @OnClick(R.id.loginbutton)
    public void loginToMain() {

        if (userNameEdit.getText().toString().equals("")) {
            Toast.makeText(this, "请输入账号", Toast.LENGTH_LONG).show();
        } else if (sharedPreferences.getString("server", "").equals("")) {
            Toast.makeText(this, "请输入服务器地址并保存", Toast.LENGTH_LONG).show();
        } else if (sharedPreferences.getString("port", "").equals("")) {
            Toast.makeText(this, "请输入端口地址并保存", Toast.LENGTH_LONG).show();
        } else {
            Intent intent = new Intent(this, MainActivity.class);
            startActivity(intent);
            editor1.putString("userName", userNameEdit.getText().toString());
            editor1.commit();
            editor.putString("userName", userNameEdit.getText().toString());
            editor.commit();
        }

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        ButterKnife.bind(this);
        if (Build.VERSION.SDK_INT >= 23) {
            //android 6.0之后动态申请权限
            if (ActivityCompat.checkSelfPermission(this, Manifest.permission.RECORD_AUDIO) != PackageManager.PERMISSION_GRANTED || ActivityCompat.checkSelfPermission(this, Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED || ActivityCompat.checkSelfPermission(this, Manifest.permission.SYSTEM_ALERT_WINDOW) != PackageManager.PERMISSION_GRANTED) {

                ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.RECORD_AUDIO, Manifest.permission.CAMERA, Manifest.permission.SYSTEM_ALERT_WINDOW, Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.MOUNT_UNMOUNT_FILESYSTEMS}, 1);
            }


        }

        sharedPreferences = getSharedPreferences("smartsee", Activity.MODE_PRIVATE);
        sharedPreferences1 = getSharedPreferences("sign", Activity.MODE_PRIVATE);
        editor = sharedPreferences.edit();
        editor1=sharedPreferences1.edit();
        String userName = sharedPreferences1.getString("userName", "");
        if (!userName.equals("")) {
            Intent intent = new Intent(this, MainActivity.class);
            startActivity(intent);

        }

        userNameEdit.setOnKeyListener(new View.OnKeyListener() {
            @Override
            public boolean onKey(View view, int i, KeyEvent keyEvent) {

                if (i == keyEvent.KEYCODE_ENTER) {

                    //  userNameEdit.clearFocus();
                    psdEdit.setFocusable(true);
                    psdEdit.setFocusableInTouchMode(true);
                    psdEdit.requestFocus();

                }
                return false;
            }
        });
        setting_Button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                final AlertDialogCommon settingDialog = new AlertDialogCommon(LoginActivity.this, R.layout.dialog_setting_layout);

                final EditText server_edit = settingDialog.getView().findViewById(R.id.serverUri_edit);
                final EditText port_edit = settingDialog.getView().findViewById(R.id.port_edit);
                final EditText number_edit = settingDialog.getView().findViewById(R.id.number_edit);

                final EditText mqServer_edit = settingDialog.getView().findViewById(R.id.mqServer_edit);
                final EditText mqPort_edit = settingDialog.getView().findViewById(R.id.mqPort_edit);
                final EditText mqName_edit = settingDialog.getView().findViewById(R.id.mqName_edit);
                final EditText mqPsd_edit = settingDialog.getView().findViewById(R.id.mqPsd_edit);
                final EditText number_loan_edit = settingDialog.getView().findViewById(R.id.number_loan_edit);
                final EditText number_finance_edit = settingDialog.getView().findViewById(R.id.number_finance_edit);
                port_edit.setText("5070");
                number_edit.setText(sharedPreferences.getString("serviceNumber","81001"));
                server_edit.setText("47.103.45.89");
                mqServer_edit.setText("47.103.45.89");
                mqPort_edit.setText("5672");
                mqName_edit.setText("mcu");
                mqPsd_edit.setText("bnp@1234");
                Log.e("number is",sharedPreferences.getString("number_loan","83001"));
                number_loan_edit.setText(sharedPreferences.getString("number_loan","82001"));
                number_finance_edit.setText(sharedPreferences.getString("number_finance","83001"));

                Button cancel = settingDialog.getView().findViewById(R.id.cancelAddress_button);
                Button save = settingDialog.getView().findViewById(R.id.saveAddress_button);
                cancel.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {

                        settingDialog.dismiss();
                    }
                });
                save.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        editor.putString("server", server_edit.getText().toString());
                        editor.putString("port", port_edit.getText().toString());
                        editor.putString("serviceNumber", number_edit.getText().toString());
                        editor.putString("mqServer", mqServer_edit.getText().toString());
                        editor.putString("mqPort", mqPort_edit.getText().toString());
                        editor.putString("mqName", mqName_edit.getText().toString());
                        editor.putString("mqPsd", mqPsd_edit.getText().toString());
                        editor.putString("number_loan", number_loan_edit.getText().toString());
                        editor.putString("number_finance", number_finance_edit.getText().toString());

                        editor.commit();


                        Toast.makeText(LoginActivity.this, "保存成功", Toast.LENGTH_LONG).show();
                        settingDialog.dismiss();
                    }
                });
                settingDialog.showDialog();

            }
        });
    }

}
